﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class adm_vlg_faqEdit : System.Web.UI.Page
{
    VLGDataContext _db8 = new VLGDataContext();
    FAQ faq = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["AID"] == null)
        //    Response.Redirect("login.aspx");
        if (IsPostBack) return;
        if (Request.QueryString["action"] == "edit")
        {
            ltr_action_name.Text = "Sửa chữa";
            faq = _db8.FAQs.Select(cat => cat).Where(cat => cat.idFAQ == Convert.ToInt32(Request.QueryString["id"])).FirstOrDefault();
        }
        else
        {

            ltr_action_name.Text = "Thêm mới";
        }
        FillData();
    }
    public void FillData()
    {
        if (faq != null)
        {
            txtQuestion.Text = faq.Question;
            txtAnswer.Text = faq.Answer;
            ddlType.SelectedValue = faq.Type.ToString();
        }
    }
    protected void butt_save_Click(object sender, EventArgs e)
    {
        if (faq == null)
            faq = new FAQ();
        if (Request.QueryString["action"] == "edit")
        {
            faq = _db8.FAQs.Select(cat => cat).Where(cat => cat.idFAQ == Convert.ToInt32(Request.QueryString["id"])).FirstOrDefault();
        }
        else
        {
            faq.DateQuestion = DateTime.Now;
            faq.IsAdminCreated = true;
        }
        faq.Type = Convert.ToInt32(ddlType.SelectedValue);
        faq.DateAnswer = DateTime.Now;
        faq.Question = txtQuestion.Text;
        faq.Answer = txtAnswer.Text;

        if (Request.QueryString["action"] == "add")
        {
            _db8.FAQs.InsertOnSubmit(faq);
        }
        _db8.SubmitChanges();
        Response.Redirect("faqs.aspx");
    }
}